using Microsoft.UI.Xaml.Input;
using Windows.System;

namespace MultiAgentsClient.Presentation.Component;

public sealed partial class AgentHeaderPanel : UserControl
{
    public AgentHeaderPanel()
    {
        InitializeComponent();
        DataContext = new BindableAgentHeaderPanelModel();
    }

    private void TextBlock_Tapped(object sender, TappedRoutedEventArgs e)
    {
        AgentNameTextBlock.Visibility = Visibility.Collapsed;
        EditAgentNameTextBox.Visibility = Visibility.Visible;
    }

    private void OnEditAgentNameTextBoxKeyDown(object sender, KeyRoutedEventArgs e)
    {
        if (e.Key == VirtualKey.Enter)
        {
            var dataContext = (BindableAgentHeaderPanelModel)DataContext;
            if (dataContext != null && string.IsNullOrEmpty(EditAgentNameTextBox.Text) == false)
            {
                dataContext.AgentName = EditAgentNameTextBox.Text;
            }
            
            AgentNameTextBlock.Visibility = Visibility.Visible;
            EditAgentNameTextBox.Visibility = Visibility.Collapsed;
        }
    }

    private void OnEditAgentNameTextBoxLostFocus(object sender, RoutedEventArgs e)
    {
        var dataContext = (BindableAgentHeaderPanelModel)DataContext;
        if (dataContext != null && string.IsNullOrEmpty(EditAgentNameTextBox.Text) == false)
        {
            dataContext.AgentName = EditAgentNameTextBox.Text;
        }

        AgentNameTextBlock.Visibility = Visibility.Visible;
        EditAgentNameTextBox.Visibility = Visibility.Collapsed;
    }
}
